//********************************************************************************************************
// Product Name: MapWindow.dll Alpha
// Description:  The basic module for MapWindow version 6.0
//********************************************************************************************************
// The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License"); 
// you may not use this file except in compliance with the License. You may obtain a copy of the License at 
// http://www.mozilla.org/MPL/ 
//
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF 
// ANY KIND, either expressed or implied. See the License for the specificlanguage governing rights and 
// limitations under the License. 
//
// The Original Code is from MapWindow.dll version 6.0
//
// The Initial Developer of this Original Code is Ted Dunsford. Created 4/4/2009 2:46:31 PM
// 
// Contributor(s): (Open source contributors should list themselves and their modifications here). 
//
//********************************************************************************************************

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using MapWindow;
using MapWindow.Main;
using MapWindow.Data;
using MapWindow.Drawing;
using MapWindow.Geometries;

namespace MapWindow.DirectX
{


    /// <summary>
    /// GroupDX
    /// </summary>
    public class GroupDX : Group, IGroupDX
    {
        #region Private Variables

        #endregion

        #region Constructors

        /// <summary>
        /// Creates a new instance of GroupDX
        /// </summary>
        public GroupDX()
        {

        }

        /// <summary>
        /// Creates a group that sits in a layer list and uses the specified progress handler
        /// </summary>
        /// <param name="container">the layer list</param>
        /// <param name="frame"></param>
        /// <param name="progressHandler">the progress handler</param>
        public GroupDX(ICollection<ILayer> container, IMapFrameDX frame, IProgressHandler progressHandler)
            : base(container, frame, progressHandler)
        {
            Layers = new LayerCollectionDX(frame, this);
          
        }


        #endregion

        /// <summary>
        /// Gets the collection of Geographic drawing layers.
        /// </summary>
        public new ILayerCollectionDX Layers
        {
            get { return base.Layers as ILayerCollectionDX; }
            set { base.Layers = value as ILayerCollection; }
        }

        /// <summary>
        /// Gets the MapFrame that this group ultimately belongs to.  This may not
        /// be the immediate parent of this group.
        /// </summary>
        public new IMapFrameDX MapFrame
        {
            get { return base.MapFrame as IMapFrameDX; }
            set { base.MapFrame = value; }
        }

        /// <summary>
        /// Adjusts the group formation to produce a GroupDX instead
        /// </summary>
        protected override void OnCreateGroup()
        {
            GroupDX grp = new GroupDX(Layers, MapFrame, ProgressHandler);
            grp.LegendText = "New Group";
        }




        #region IRenderableDX Members

        /// <summary>
        /// After Drawing
        /// </summary>
        public event EventHandler<DrawCompletedArgsDX> DrawingCompleted;

        /// <summary>
        /// Before Drawing..
        /// </summary>
        public event EventHandler<DrawVerifyArgsDX> BeforeDrawing;

        /// <summary>
        /// Draws all the layers in this group
        /// </summary>
        /// <param name="args"></param>
        public void Draw3D(DrawArgsDX args)
        {
            if (OnBeforeDrawing(args) == true)
            {
                OnDrawingCompleted(args, true);
            }
            foreach (ILayerDX layer in Layers)
            {
                layer.Draw3D(args);
            }
            OnDrawingCompleted(args, false);
        }

        /// <summary>
        /// Initializes all the layers in this group
        /// </summary>
        /// <param name="args"></param>
        public void Initialize(DrawArgsDX args)
        {
            foreach (ILayerDX layer in Layers)
            {
                layer.Initialize(args);
            }
        }

        /// <summary>
        /// Not sure what this needs to be at the moment.
        /// </summary>
        public int NumParts
        {
            get { return 1; }
        }

        /// <summary>
        /// Fires on Drawing Completed
        /// </summary>
        /// <param name="args"></param>
        /// <param name="canceled"></param>
        protected virtual void OnDrawingCompleted(DrawArgsDX args, bool canceled)
        {
            if (DrawingCompleted != null) DrawingCompleted(this, new DrawCompletedArgsDX(args, canceled, null));
        }

        /// <summary>
        /// Fires BeforeDrawing
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        protected virtual bool OnBeforeDrawing(DrawArgsDX args)
        {
            bool cancel = false;
            if (BeforeDrawing != null) BeforeDrawing(this, new DrawVerifyArgsDX(args, cancel));
            return cancel;
        }

        #endregion
    }
}
